package com.market.sys.api.admin;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.market.sys.common.Result;
import com.market.sys.dto.CommentSearchDto;
import com.market.sys.entity.Comment;
import com.market.sys.entity.SubComment;
import com.market.sys.entity.User;
import com.market.sys.service.IArticleService;
import com.market.sys.service.IBoothService;
import com.market.sys.service.ICommentService;
import com.market.sys.service.ISubCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <b></b>
 *
 * @Author duxinlei@yunjinhz.com
 * @Company 北京云锦汇智信息技术有限公司
 * @Version v1.0
 * @Date 2024/3/28
 */
@RestController
@RequestMapping("/sys/admin/subComment")
public class AdminSubCommentController {

    @Autowired
    private ISubCommentService subCommentService;

    @Autowired
    private ICommentService commentService;

    @Autowired
    private IBoothService boothService;

    @Autowired
    private IArticleService articleService;

    @PostMapping("/save")
    public Result<?> save(@RequestBody SubComment subComment){
        if (Objects.isNull(subComment)){
            return Result.error("提交信息不能为空！");
        }
        boolean result = subCommentService.saveOrUpdate(subComment);
        if (result){
            return Result.success("操作成功");
        }
        return Result.error("操作失败");
    }

    @PostMapping("/list")
    public Result<Map<String ,Object>> search(@RequestBody CommentSearchDto form){
        MPJLambdaWrapper<SubComment> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(SubComment.class);
        wrapper.leftJoin(User.class, User::getId, SubComment::getUserId);

        wrapper.selectAs(User::getNickName, SubComment::getNickName);
        wrapper.selectAs(SubComment::getContent, SubComment::getCommentContent);

        Page<SubComment> page = subCommentService.page(new Page<>(form.getPageNo(), form.getPageSize()), wrapper);

        Map<String,Object> data = new HashMap<>();
        List<SubComment> subComments = page.getRecords();
        List<SubComment> records = new ArrayList<>();
        if (CollUtil.isNotEmpty(subComments)){
            for (SubComment record : subComments) {
                Comment comment = commentService.getById(record.getParentCommentId());
                Integer type = comment.getType();
                Integer thingId = comment.getThingId();
                if (type == 1){
                    record.setThingName(articleService.getById(thingId).getTitle());
                }
                if (type == 2){
                    record.setThingName(boothService.getById(thingId).getBoothName());
                }
                if (form.getType() != null){
                    if (type == form.getType()){
                        records.add(record);
                    }
                } else {
                    records.add(record);
                }
            }
        }
        data.put("total",page.getTotal());
        data.put("rows",records);

        return Result.success(data);
    }
}
